//author:kakake
class tshomeDao {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        dbConnBoot: TSLobsterAPI.Program.GetDbConn("LobsterBoot"),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
    }

    getuser(code: string) {
        let strsql = "select * from BaseUser where Code=@code";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { code: code });
    }

    getdefaultdept(empid) {
        let strsql = "select distinct b.* from BaseEmpDept a left join basedept b on a.deptid=b.deptid where a.DefaultFlag=1 AND b.DeptId IS NOT NULL and a.EmpId=@empid";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { empid: empid });
    }

    getbaseemployee(empid) {
        let strsql = "select * from BaseEmployee where EmpId=@empid";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { empid: empid });
    }

    getbaseworker(workid) {
        let strsql = "select * from BaseWorkers where WorkId=@workid";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { workid: workid });
    }
    /**用户系统模块 */
    getmodulelist(userid, isadmin) {
        if (parseInt(isadmin) != 0) {
            let strsql = "select * from BaseModule";
            return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);
        } else {
            let strsql = "select distinct c.* from BaseGroupUser a INNER join BaseGroupMenu b on a.GroupId=b.GroupId \
                                            INNER join BaseModule c on b.ModuleId=c.ModuleId \
                                            where a.UserId=@userid";
            return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { userid: userid });
        }
    }
    /**用户菜单 */
    getmenulist(userid, isadmin) {
        if (parseInt(isadmin) != 0) {
            let strsql = "select c.MenuId,c.SortId,c.Name,c.DllName SoftName,c.FunName ControllerName,c.FunWcfName ViewName,c.ModuleId,c.PMenuId,c.MenuToolBar,c.MenuLookBar,c.Memo,c.Image,c.UrlId,c.UrlName,c.BindSQL,c.WorkId from BaseMenu c";
            return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);
        } else {
            let strsql = "select distinct c.MenuId,c.SortId,c.Name,c.DllName SoftName,c.FunName ControllerName,c.FunWcfName ViewName,c.ModuleId,c.PMenuId,c.MenuToolBar,c.MenuLookBar,c.Memo,c.Image,c.UrlId,c.UrlName,c.BindSQL,c.WorkId from BaseGroupUser a left join BaseGroupMenu b on a.GroupId=b.GroupId\
                                            left join BaseMenu c on b.MenuId=c.MenuId\
                                            where a.UserId=@userid and c.MenuId is not null";
            return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { userid: userid });
        }
    }
    /**人员所属科室 */
    gethavedeptlist(empid) {
        let strsql = "select distinct b.* from BaseEmpDept a left join BaseDept b on a.DeptId=b.DeptId where a.EmpId=@empid and b.DeptId is not null";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { empid: empid });
    }

    /**获取报表配置 */
    getReportBasicData() {
        let strsql = "SELECT ID,FileName as ReportTitle,EnumValue FROM Basic_ReportConfig WHERE WorkID=@workid ORDER BY EnumValue";
        //TSLobsterAPI.Utils.TrackLog("workid",this.data.WorkId);
        return TSLobsterAPI.DbHelper.DbGetJsonTable(this.data.dbConn, strsql, { workid: this.data.WorkId });
    }

    /**获取未读消息 */
    getNotReadMessages(empid) {
        let strsql = "SELECT a.Id,b.MessageTypeCode,b.MessageTitle,b.MessageContent FROM BaseMessageRead a\
                                LEFT JOIN BaseMessage b ON a.MessageId=b.Id\
                                WHERE a.EmpId=@empid AND IsRead=0 AND GETDATE()< DATEADD(DAY,b.Limittime,a.SendTime)";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { empid: empid });
    }

    /**设置消息已读 */
    setMessageRead(ids) {
        let strsql = "UPDATE BaseMessageRead SET IsRead=1 WHERE Id IN (" + ids + ")";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql);
    }

    /**获取用户所有应用 */
    geAllBaseModule(ssouserid) {
        let strsql = "SELECT DISTINCT  f.* FROM \
	                                App_BaseGroup a\
	                                LEFT JOIN App_BaseGroupMenu b ON a.GroupId= b.GroupId\
	                                RIGHT JOIN App_BaseMenu e ON b.MenuId= e.MenuId\
	                                LEFT JOIN App_BaseModule f ON e.ModuleId=f.ModuleId\
                                    inner join ( \
	                                    SELECT\
		                                    b.AppId\
	                                    FROM\
		                                    BaseAppEmpGroup a\
		                                    LEFT JOIN BaseApp b ON a.AppId= b.AppId  AND a.WorkId= b.WorkId  \
	                                    WHERE\
		                                    b.DelFlag= 0 \
		                                    AND a.WorkId= @WorkId \
		                                    AND a.EmpId= dbo.fnGetEmpId ( @SSOUserId, @WorkId )    \
	                                    )  as c on c.AppId=f.AppId  \
                                WHERE   e.DelFlag= 0  ORDER BY f.SortId,f.ModuleId";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConnBoot, strsql, { SSOUserId: ssouserid, WorkId: this.data.WorkId });
    }

     /**获取用户所有菜单 */
    getAllBaseClass(ssouserid,moduleid,pid) {
        let strsql = "SELECT DISTINCT\
	                            e.DelFlag ,\
	                            e.BindSQL ,\
	                            e.UrlName,\
	                            e.UrlId ,\
	                            e.Image ,\
	                            e.Memo ,\
	                            e.MenuLookBar ,\
	                            e.MenuToolBar ,\
	                            e.MenuType ,\
	                            e.PMenuId ,\
	                            e.ModuleId ,\
	                            e.ControllerName ,\
	                            e.ViewName ,\
	                            e.SoftName ,\
	                            e.Name ,\
	                            e.SortId ,\
	                            e.MenuId,\
	                            e.UserLevels \
                            FROM\
	                            App_BaseGroup a\
	                            LEFT JOIN App_BaseGroupMenu b ON a.GroupId= b.GroupId\
	                            LEFT JOIN App_BaseMenu e ON b.MenuId= e.MenuId\
	                            LEFT JOIN (SELECT t.Id,(case (SELECT WorkType FROM BaseWorkers WHERE WorkId=@WorkId) WHEN 0 THEN t.Url WHEN 1 THEN t.DEVUrl ELSE t.UATUrl END) Url FROM  App_BaseWebSite t) f ON e.UrlId= f.Id and e.MenuType=2\
                            WHERE\
	                            e.DelFlag= 0 \
                                and e.ModuleId=@ModuleId \
                                and e.PMenuId=@PMenuId \
                                and e.MenuType=0\
	                            AND a.GroupId IN ( SELECT GroupId FROM BaseAppEmpGroup a \
                                                        LEFT JOIN BaseApp b on a.AppId=b.AppId and a.WorkId=b.WorkId\
                                                        WHERE b.DelFlag=0 and a.WorkId= @WorkId AND a.EmpId= dbo.fnGetEmpId (@SSOUserId,@WorkId) ) \
                            ORDER BY\
	                            e.ModuleId,\
	                            e.SortId";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConnBoot, strsql, { SSOUserId: ssouserid,ModuleId:moduleid,PMenuId:pid, WorkId: this.data.WorkId });
    }

    /**获取用户所有菜单 */
    getAllBaseMenu(ssouserid,moduleid,pid) {
        let strsql = "SELECT DISTINCT\
	                            e.DelFlag ,\
	                            e.BindSQL ,\
	                            (f.Url+e.UrlName) UrlName,\
	                            e.UrlId ,\
	                            e.Image ,\
	                            e.Memo ,\
	                            e.MenuLookBar ,\
	                            e.MenuToolBar ,\
	                            e.MenuType ,\
	                            e.PMenuId ,\
	                            e.ModuleId ,\
	                            e.ControllerName ,\
	                            e.ViewName ,\
	                            e.SoftName ,\
	                            e.Name ,\
	                            e.SortId ,\
	                            e.MenuId,\
	                            e.UserLevels \
                            FROM\
	                            App_BaseGroup a\
	                            LEFT JOIN App_BaseGroupMenu b ON a.GroupId= b.GroupId\
	                            LEFT JOIN App_BaseMenu e ON b.MenuId= e.MenuId\
	                            LEFT JOIN (SELECT t.Id,(case (SELECT WorkType FROM BaseWorkers WHERE WorkId=@WorkId) WHEN 0 THEN t.Url WHEN 1 THEN t.DEVUrl ELSE t.UATUrl END) Url FROM  App_BaseWebSite t) f ON e.UrlId= f.Id and e.MenuType=2\
                            WHERE\
	                            e.DelFlag= 0 \
                                and e.ModuleId=@ModuleId \
                                and e.PMenuId=@PMenuId \
                                and e.MenuType<>0\
	                            AND a.GroupId IN ( SELECT GroupId FROM BaseAppEmpGroup a \
                                                        LEFT JOIN BaseApp b on a.AppId=b.AppId and a.WorkId=b.WorkId\
                                                        WHERE b.DelFlag=0 and a.WorkId= @WorkId AND a.EmpId= dbo.fnGetEmpId (@SSOUserId,@WorkId) ) \
                            ORDER BY\
	                            e.ModuleId,\
	                            e.SortId";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConnBoot, strsql, { SSOUserId: ssouserid,ModuleId:moduleid,PMenuId:pid, WorkId: this.data.WorkId });
    }
}